import pandas as pd


def zero_prediction(data_test):
    data_test['repost_hat'] = 0
    data_test['comments_hat'] = 0
    data_test['likes_hat'] = 0
    return data_test


# 计算MSE
def MSE(data_test):
    subset = data_test[['repost', 'comments', 'likes', 'repost_hat', 'comments_hat', 'likes_hat']]
    MSE_set = []
    for ii in ['repost', 'comments', 'likes']:
        MSE = sum((subset[ii] - subset[(ii + '_hat')]) ** 2) / len(subset[ii])
        MSE_set.append(MSE)
    return MSE_set


def precision(data):
    data['deviation_repost'] = list(map(lambda x, y: abs(x - y) / (y + 5), data['repost_hat'], data['repost']))
    # print (data['deviation_repost'])
    data['deviation_likes'] = list(map(lambda x, y: abs(x - y) / (y + 3), data['likes_hat'], data['likes']))
    # print (data['deviation_likes'])
    data['deviation_comments'] = list(map(lambda x, y: abs(x - y) / (y + 3), data['comments_hat'], data['comments']))
    # print (data['deviation_comments'])
    data['lcf_sum'] = data['repost'] + data['likes'] + data['comments']
    #    print (data['lcf_sum'])
    data['lcf_sum'] = data['lcf_sum'].apply(lambda x: 100 if x > 100 else x)
    data['precision_1_-0.8'] = 1 - 0.5 * data['deviation_repost'] - 0.25 * data['deviation_likes'] - 0.25 * data[
        'deviation_comments'] - 0.8
    # print (data['precision_1_-0.8'])
    data.loc[data['precision_1_-0.8'] <= 0, 'sgn'] = 0
    data.loc[data['precision_1_-0.8'] > 0, 'sgn'] = 1
    #    print (data['sgn'])
    precision_ = sum((data['lcf_sum'] + 1) * data['sgn']) / sum(data['lcf_sum'] + 1)
    return precision_


data_valid = pd.read_csv('data_predict.txt', index_col=[0], header=0)

data_valid = zero_prediction(data_valid)

precision(data_valid)
